﻿using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using CastleExample.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CastleExample
{
    class Program
    {


        static void Main(string[] args)
        {
            #region Castle.Core
            ProxyGenerator generator = new ProxyGenerator(); // 实例化代理生成器
            Interceptor interceptor = new Interceptor(); //实例化拦截器

            //使用代理生成器创建Person对象， 而不是使用new关键字实例化
            //TestInterceptor test = generator.CreateClassProxy<TestInterceptor>(interceptor);
            //Console.WriteLine("当前类型: {0}, 父类型: {1}", test.GetType(), test.GetType().BaseType);
            //Console.WriteLine();
            //test.MethodInterceptor();
            //Console.WriteLine();
            //test.NoInterceptor();
            //Console.WriteLine();
            //Console.ReadLine();


            /*  动态代理
            //使用【代理类生成器】创建Person对象，而不是使用new关键字来实例化
            Person person = generator.CreateClassProxy<Person>(interceptor);


            Console.WriteLine("当前类型:{0},父类型:{1}", person.GetType(), person.GetType().BaseType);
            Console.WriteLine();


            person.SayHello();//跟普通调用没有两样吧？
            Console.WriteLine();

            person.SayName("福建");//跟普通调用没有两样吧？
            Console.WriteLine();

            person.SayOther();//它不是虚方法，无法拦截。待会检测输出情况就知道了。   



            Console.ReadLine();
 
            */


            var container = new WindsorContainer();
            container.Register(Classes.FromAssemblyNamed("CastleExample")
                            .Where(type => type.Name.EndsWith("Repository")).WithServiceDefaultInterfaces().LifestyleTransient());
            //container.Register(Classes.FromAssemblyNamed("Repository")
            //                .Where(type => type.Name.EndsWith("Repository")).WithServiceDefaultInterfaces().LifestyleTransient());
            //container.Register(Classes.FromAssemblyNamed("IService")
            //                .Where(type => type.Name.EndsWith("Service")).WithServiceDefaultInterfaces().LifestyleTransient());
            container.Register(Classes.FromAssemblyNamed("CastleExample")
                            .Where(type => type.Name.EndsWith("Service")).WithServiceDefaultInterfaces().LifestyleTransient());

            container.Register(Component.For<TestIOCClass>());

            var test = container.Resolve<TestIOCClass>();
            test.Say();

            Console.ReadLine();

            #endregion
        }
    }
}
